﻿using System;
using System.Net;
using System.Web;

public partial class ACPanel_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ipAdd = GetIpAddress();
            string compName = GetCompCode();
            string publicIp = GetPublicIP();

            // Tao moi cookie
            var cookie = new HttpCookie("ipAddress");
            cookie.Values.Add("ipaddress", ipAdd);
            cookie.Values.Add("compName", compName);
            cookie.Values.Add("publicIp", publicIp);

            cookie.Expires = DateTime.Now.AddMinutes(15);

            // write cookie to client
            Response.Cookies.Add(cookie);
        }
    }

    public static string GetIpAddress()  // Get IP Address
    {
        string ip = "";
        IPHostEntry ipEntry = Dns.GetHostEntry(GetCompCode());
        IPAddress[] addr = ipEntry.AddressList;
        ip = addr[2].ToString();
        return ip;
    }
    public static string GetCompCode()  // Get Computer Name
    {
        string strHostName = "";
        strHostName = Dns.GetHostName();
        return strHostName;
    }

    protected string GetPublicIP()
    {
        string visitorsIpAddr = string.Empty;
        if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            visitorsIpAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        else if (!string.IsNullOrEmpty(HttpContext.Current.Request.UserHostAddress))
        {
            visitorsIpAddr = HttpContext.Current.Request.UserHostAddress;
        }
        return visitorsIpAddr;
    }
}